//
//  LoginViewController.swift
//  cloud
//
//  Created by 吳瀾洲 on 2020/7/2.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

import UIKit
import ActiveLabel

class LoginViewController: UIViewController {
    
    private var txtUser:UITextField?;
    private var txtPwd:UITextField?;

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white;
        
        let url:String = URLUtils.shared.getRequestURL(method: "login");
        print("url = \(url)")
        
        print("kingboyrang")
        //设置部分背景色
        let bgView:UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: self.view.width, height: self.view.height/2.0 - 150));
        bgView.backgroundColor = UIColor.withRGBA(0, 180, 252);
        self.view.addSubview(bgView);
        
        
        //设置按钮
        let topY:CGFloat = self.view.height > 667 ? 20.0:0.0;
        let setImg:UIImage = UIImage.init(named: "setting")!;
        let setBtn:UIButton = UIButton.init(frame: CGRect(x: self.view.width - setImg.size.width - 15, y: 30 + topY, width: setImg.size.width, height: setImg.size.height));
        setBtn.setImage(setImg, for: .normal)
        self.view.addSubview(setBtn);
        
        //添加标题(20)
        let h:CGFloat = self.view.height > 667 ? 0.0:20.0;
        let fh:CGFloat = self.view.height > 667 ? 10.0:0.0;
        let labTitle:UILabel = UILabel.init(frame: CGRect(x: -self.view.width, y: (bgView.frame.size.height - 40)/2 - h, width: bgView.frame.size.width, height: 40));
        labTitle.text = "云桌面";
        //labTitle.text = "用户登录";
        labTitle.textColor = UIColor.white;
        labTitle.textAlignment = .center;
        labTitle.backgroundColor = UIColor.clear;
        labTitle.shadowColor = UIColor.withRGBA(211, 211, 211);
        labTitle.shadowOffset = CGSize(width: 2, height: 2);
        //labTitle.layer.opacity = 0.95
        labTitle.font = UIFont.init(name: "Helvetica-Bold", size: 30 + fh);//50
        bgView.addSubview(labTitle)
        
        //登录页面
        //let loginView = createLoginView(top: bgView.frame.maxY - 30);
        let loginView = createLoginView(top: self.view.height);
        self.view.addSubview(loginView);
        
        //头像
       // let imgView:UIImageView = UIImageView.init(frame: CGRect(x: (self.view.width - 100)/2.0, y: loginView.y - 50, width: 100, height: 100));
         let imgView:UIImageView = UIImageView.init(frame: CGRect(x: (self.view.width - 100)/2.0, y: -100, width: 100, height: 100));
        imgView.image = UIImage.init(named: "header");
        self.view.addSubview(imgView);
        
        //协议
        let footer:ActiveLabel = ActiveLabel.init(frame: CGRect(x: self.view.width, y: self.view.height - 40 - 30, width: self.view.width, height: 40))
        footer.textAlignment = .center;
        let customType = ActiveType.custom(pattern: "<<登录服务使用协议>>") 
        let customType2 = ActiveType.custom(pattern: "<<隐私政策>>")
        footer.enabledTypes.append(customType)
        footer.enabledTypes.append(customType2)
        
        footer.customize { label in
            label.text = "点击登录表示同意 <<登录服务使用协议>> 和 <<隐私政策>>"
            label.numberOfLines = 0
            label.lineSpacing = 4
            label.font = UIFont.systemFont(ofSize: 12)
            label.textColor = UIColor.black;
            //Custom types
            label.customColor[customType] = UIColor.withRGBA(0, 180, 252)
            label.customSelectedColor[customType] = UIColor.withRGBA(0, 180, 252)
            label.customColor[customType2] = UIColor.withRGBA(0, 180, 252)
            label.customSelectedColor[customType2] = UIColor.withRGBA(0, 180, 252)
            label.handleCustomTap(for: customType) {
                
                self.alert("Custom type", message: $0)
                
            }
            label.handleCustomTap(for: customType2) {
                self.alert("Custom type", message: $0)
            }
            
        }
        self.view.addSubview(footer);
        
        
        
        //动画效果
        UIView.animate(withDuration: 0.5, delay: 0, options: .transitionFlipFromLeft, animations: {
             labTitle.x = 0;
        }) { (finished:Bool) in
            
            if(finished){
                
                UIView.animate(withDuration: 0.5, delay: 0, options: .transitionFlipFromRight, animations: {
                     footer.x = 0;
                }) { (end:Bool) in
                    if(end){
                        self.viewAnmianl(bgView: bgView, header: imgView, loginView: loginView);
                    }
                }
                
            }
            
        };
    }
    
    func alert(_ title: String, message: String) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        vc.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }
    
   
    
    //登录
    func createLoginView(top:CGFloat) -> UIView {
        
        let loginView:UIView = UIView.init(frame: CGRect(x: 20,y: top,width: self.view.width-40,height: 350));
        
        loginView.backgroundColor = UIColor.white;
        loginView.layer.cornerRadius = 10.0;
        loginView.layer.borderWidth = 0.5;
        loginView.layer.borderColor = UIColor.lightGray.cgColor;
        loginView.layer.masksToBounds = true
        
        loginView.layer.shadowColor = UIColor.lightGray.cgColor;
        loginView.layer.shadowOffset = CGSize(width: 5, height: 5);
        loginView.layer.shadowRadius = 0.6
        loginView.layer.shadowOpacity = 0.8
        //loginView.layer.opacity = 0.8;
        
        
        //用户名
        txtUser = UITextField.init(frame: CGRect(x: 20, y: 70, width: loginView.width - 40, height: 50));
        txtUser?.backgroundColor = UIColor.withRGBA(230, 230, 230);
        txtUser?.layer.cornerRadius = 25;
        txtUser?.textAlignment = .center;
        txtUser?.placeholder = "用户名";
        loginView.addSubview(txtUser!)
        
        //密码
           txtPwd = UITextField.init(frame: CGRect(x: 20, y: txtUser!.frame.maxY + 20, width: loginView.width - 40, height: 50));
           txtPwd?.backgroundColor = UIColor.withRGBA(230, 230, 230);
           txtPwd?.layer.cornerRadius = 25;
           txtPwd?.textAlignment = .center;
           txtPwd?.isSecureTextEntry = true;
           txtPwd?.placeholder = "密码";
           loginView.addSubview(txtPwd!)
        
        //登录
        let loginBtn:UIButton = UIButton.init(frame: CGRect(x: 20, y: txtPwd!.frame.maxY + 20, width: loginView.width - 40, height: 50));
        loginBtn.backgroundColor = UIColor.withRGBA(0, 180, 252);
        loginBtn.layer.cornerRadius = 25;
        loginBtn.setTitle("登录", for: .normal);
        loginBtn.setTitleColor(UIColor.white, for: .normal);
        loginBtn.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 20);
        loginView.addSubview(loginBtn)
        
        loginView.height = loginBtn.frame.maxY + 40;
        //同意协议
        
        //header
        return loginView;
        
    }
    
    //旋转360度
    func transform(view:UIView)
    {
        
         // 1. 创建动画
         let rotationAnim = CABasicAnimation(keyPath: "transform.rotation.z")
         // 2. 设置动画属性
         rotationAnim.fromValue = 0 // 开始角度
         rotationAnim.toValue = Double.pi * 2 //结束角度
         rotationAnim.repeatCount = 0 // 重复次数
        rotationAnim.duration = 0.5 // 一圈所需要的时间
         rotationAnim.isRemovedOnCompletion = false //默认是true，切换到其他控制器再回来，动画效果会消失，需要设置成false，动画就不会停了
         view.layer.add(rotationAnim, forKey: "transform.rotation.z");
    }
    
    //移动效果
    func viewAnmianl(bgView:UIView,header:UIView,loginView:UIView) -> Void {

        UIView.animate(withDuration: 1.0, animations: {
            //登录
            loginView.y = bgView.frame.maxY - 30;
            //头像
            header.y = loginView.y - 50;
        }) { (finished:Bool) in
            if(finished){
                self.transform(view: header);
            }
        }
      
    }
    
    //隐藏键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.txtUser?.resignFirstResponder();
        self.txtPwd?.resignFirstResponder();
    }
    
   
    
        
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
